﻿/*
*
* 全局屏幕点击特效
*
*
*/


using UnityEngine;

using UnityEngine.UI;

namespace SG
{
    public class ScreenTapFxMgr
    {

        //public const string Path = "UI/Prefabs/ScreenTapFx/ScreenTapFXUI";
        // public const string CameraPath = "UI/Prefabs/ScreenTapFx/ScreenTapFXUICamera";

        public const string Layer = "Global";

        public const int SortingOrder = 20000;

        private static ScreenTapFxMgr m_instance;

        GameObject mRoot;
        Camera mCamera;
        public static ScreenTapFxMgr Instance()
        {
            if (m_instance == null)
            {
                m_instance = new ScreenTapFxMgr();
            }
            return m_instance;
        }

        public void DoInit()
        {
            CreateScreenTapFxUI();
        }

        public void CreateScreenTapFxUI()
        {
            if (mRoot != null)
                return;

            mRoot = GameObject.Find("GlobalScreenTapRoot");
            if (mRoot == null)
            {
                XKLog.LogError("GlobalScreenTapRoot is null");
                return;
            }

            GameObject.DontDestroyOnLoad(mRoot);

            var camera = mRoot.FindDeepChildByName("ScreenTapFXUICamera");
            camera.transform.SetParent(mRoot.transform);
            mCamera = camera.GetComponent<Camera>();
            SetCamera();
            var window = mRoot.FindDeepChildByName("ScreenTapFXUI");
            if (window != null)
            {
                window.transform.SetParent(mRoot.transform);
                RectTransform rectTrans = window.transform as RectTransform;
                rectTrans.localPosition = Vector3.zero;
                rectTrans.localScale = Vector3.one;
                DoInitWindow(window, mCamera);
            }
            else
                XKLog.LogError("CreateWindow error: ScreenTapFXUI");
        }

        void DoInitWindow(GameObject window, Camera camera)
        {
            var scaler = window.GetComponent<CanvasScaler>();
            scaler.referenceResolution = new Vector2(1920, 1080);
            //scaler.matchWidthOrHeight = UIScreenAdaptationMgr.Instance().GetCanvasScalerMatch();

            var canvas = window.GetComponent<Canvas>();
            canvas.renderMode = RenderMode.ScreenSpaceCamera;
            canvas.worldCamera = camera;

            canvas.overrideSorting = true;
            canvas.sortingOrder = SortingOrder;

            window.SetAllChildLayer(Layer);

            //ScreenTapFX screenTapFX = window.GetComponent<ScreenTapFX>();
            //screenTapFX.fxRenderCamera = camera;
            //screenTapFX.mSortOrder = SortingOrder;
        }
        public Camera GetCamera()
        {
            return mCamera;
        }

        public void CheckCamera()
        {
            if (mCamera == null)
                return;
            if (Camera.main == null)
                return;
            //CameraMgr 处理
            /*            var uacd = Camera.main.GetUniversalAdditionalCameraData();
                        if (uacd.renderType != CameraRenderType.Base)
                            return;

                        if (uacd && uacd.cameraStack != null)
                        { 
                            if (!uacd.cameraStack.Contains(mCamera))
                                uacd.cameraStack.Remove(mCamera);
                            uacd.cameraStack.Add(mCamera);
                        } */
        }
        public void SetCamera()
        {
            if (mCamera == null)
                return;

            //if (mCamera.gameObject.TryGetComponent(out UniversalAdditionalCameraData camData)) 
            //    camData.renderPostProcessing = GameSettingManager.Instance().CameraPostProcessing();        
        }
    }
}